package net.zhengxinyun.performance.controller.exam;


import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.ExamManageEntity;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.filter.ExamIndexFilter;
import net.zhengxinyun.performance.filter.ExamManageFilter;
import net.zhengxinyun.performance.service.exam.examClass.ExamIndexService;
import net.zhengxinyun.performance.service.exam.examClass.ExamManageService;
import net.zhengxinyun.performance.service.login.LoginService;
import net.zhengxinyun.performance.service.login.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 管理问卷是否公开测评 对于已经生成的问卷进行管理
 */
@RestController
@RequestMapping("/examManage")
public class ExamManageCtrl {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private LoginService loginService;

    @Autowired
    private ExamIndexService examIndexService;
    @Autowired
    private ExamManageService manageService;


    @PostMapping("/query")
    public Result<HashMap<String,Object>> query(@RequestBody ExamManageParam param)
    {

            HashMap<String,Object> map = new HashMap<>();
            List<ExamIndexFilter> query = examIndexService.query(new ExamIndexFilter());

            List<ExamManageFilter> list = manageService.query(param.filter);

            int total = manageService.queryForCount(param.filter);
        //转换多选测评对象
        for (ExamManageFilter item : list) {
            List<String> strings = Util.strToList(item.getExamer());
            String examer="";
            for (String string : strings) {
                examer =Util.examerMap.get(string)+";"+examer;
            }
            item.setExamer(examer);
            item.setListExamers(strings);

        }

            map.put("examList",query);
            map.put("list",list);
            map.put("pageTotal",total);
            map.put("userType","false".equals(param.tokenEntity.getValue())?"0":null  );

            return ResultUtils.getResults(Result.Status.SUCCESS,"",map);


    }

   /* private HashMap<String,Object> getExamList(ExamManageParam param)
    {

        HashMap<String,Object> map = new HashMap<>();
        List<ExamIndexFilter> query = examIndexService.query(new ExamIndexFilter());

        List<ExamManageFilter> list = manageService.query(param.filter);

        int total = manageService.queryForCount(param.filter);

        map.put("examList",query);
        map.put("list",list);
        map.put("pageTotal",total);

        return map;
    }
    */

    /**
     * 添加问卷
     * @param param
     * @return
     */
    @PostMapping("/insert")
    public Result<String> insertManage(@RequestBody ExamManageParam param)
    {
        if(tokenService.compareToken(param.tokenEntity.getValue())){
            String examer="";
            Long examId = param.entity.getExamId();
            if(ObjectUtils.isBlank(examId))
            {
                ResultUtils.getResults(Result.Status.ERROR,"examId空值，请重新选择问卷");
            }
            param.entity.setId(null);
            List<String> listExamers = param.entity.getListExamers();
            List<String> collect = listExamers.stream().filter(s -> s.equals("0")).collect(Collectors.toList());
            if(ObjectUtils.isNotBlank(collect))
            {
                param.entity.setExamer("0");
            }else {
                examer= StringUtils.join(listExamers,",");
                param.entity.setExamer(examer);
            }

            int insert = manageService.insert(param.entity);
            if(insert==0)
            {
                return  ResultUtils.getResults(Result.Status.ERROR,"保存失败");
            }
            return  ResultUtils.getResults(Result.Status.SUCCESS,"操作成功");
        }
        return  ResultUtils.getResults(Result.Status.OVERTIME,"error token");
    }

    /**问卷管理更新
     *
     * @param param
     * @return
     */
    @PostMapping("/update")
    public  Result<String> updateManage(@RequestBody ExamManageParam param)
    {
        if(tokenService.compareToken(param.tokenEntity.getValue()))
        {

            List<String> listExamers = param.entity.getListExamers();
            if(ObjectUtils.isNotBlank(listExamers))
            {
                String examer ="";
                List<String> collect = listExamers.stream().filter(s -> s.equals("0")).collect(Collectors.toList());
                if(ObjectUtils.isNotBlank(collect))
                {
                    param.entity.setExamer("0");
                }else {
                    examer= StringUtils.join(listExamers,",");
                    param.entity.setExamer(examer);
                }



            }

            int insert = manageService.updateByPrimaryKey(param.entity);
            if(insert ==0){
                return ResultUtils.getResults(Result.Status.ERROR,"更新失败");
            }
            return ResultUtils.getResults(Result.Status.SUCCESS,"更新成功");
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "登录过期");
    }

    /**
     * 问卷管理删除
     * @param param
     * @return
     */
    @PostMapping("/delete")
    public  Result<String> deletetManage(@RequestBody Param param)
    {
        if(tokenService.compareToken(param.tokenEntity.getValue()))
        {
            int insert = manageService.deleteByPrimaryKey(param.entity.getId());
            if(insert ==0){
                return ResultUtils.getResults(Result.Status.ERROR,"删除失败");
            }
            return ResultUtils.getResults(Result.Status.SUCCESS,"删除成功");
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "登录过期");
    }


}




class ExamManageParam
{
    public ExamManageEntity entity;
    public ExamManageFilter filter;
    public TokenEntity tokenEntity;

}